package com.itqdkj.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itqdkj.health.constant.MessageConstant;
import com.itqdkj.health.entity.PageResult;
import com.itqdkj.health.entity.QueryPageBean;
import com.itqdkj.health.entity.Result;
import com.itqdkj.health.pojo.CheckGroup;
import com.itqdkj.health.service.CheckGroupService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@ResponseBody
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    CheckGroupService checkGroupService;



    /**
     * 新增检查组
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPageAll")
    public PageResult findPageAll(@RequestBody QueryPageBean queryPageBean){

            //1.封装总记录数
            //2.封装分页数据
        return  checkGroupService.findPageAll(queryPageBean);
    }
    @RequestMapping("/findById")
    public Result findById(@RequestParam("id") Integer checkgroup_Id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(checkgroup_Id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 使用检查组的id，查询检查项的集合
     * @param checkgroup_Id
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public List<Integer> findCheckItemIdsByCheckGroupId(@RequestParam("id") Integer checkgroup_Id){
        return checkGroupService.findCheckItemIdsByCheckGroupId(checkgroup_Id);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(@RequestParam(value = "id") Integer checkgroup_Id){
        try {
            checkGroupService.delete(checkgroup_Id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/findAll")
    public List<CheckGroup> findAll(){
        return checkGroupService.findAll();
    }
}
